//! 系统信息工具

use std::env;
use std::fs;
use std::path::Path;

/// 获取环境变量
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::get_env_var;
/// 
/// // 获取PATH环境变量
/// let path = get_env_var("PATH");
/// assert!(path.is_some());
/// 
/// // 获取不存在的环境变量
/// let nonexistent = get_env_var("NONEXISTENT_VAR_12345");
/// assert!(nonexistent.is_none());
/// ```
pub fn get_env_var(key: &str) -> Option<String> {
    env::var(key).ok()
}

/// 设置环境变量
/// 
/// 注意：这只会影响当前进程及其子进程，不会影响父进程或系统环境变量
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::set_env_var;
/// 
/// set_env_var("TEST_VAR", "test_value");
/// assert_eq!(std::env::var("TEST_VAR").unwrap(), "test_value");
/// ```
pub fn set_env_var(key: &str, value: &str) {
    env::set_var(key, value);
}

/// 获取当前工作目录
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::current_dir;
/// 
/// let dir = current_dir();
/// assert!(dir.is_ok());
/// ```
pub fn current_dir() -> Result<String, std::io::Error> {
    env::current_dir().map(|path| path.to_string_lossy().to_string())
}

/// 检查文件是否存在
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::file_exists;
/// use std::fs;
/// 
/// // 创建临时文件用于测试
/// fs::write("temp_test_file.txt", "test").unwrap();
/// 
/// assert_eq!(file_exists("temp_test_file.txt"), true);
/// assert_eq!(file_exists("nonexistent_file.txt"), false);
/// 
/// // 清理
/// std::fs::remove_file("temp_test_file.txt").unwrap();
/// ```
pub fn file_exists<P: AsRef<Path>>(path: P) -> bool {
    Path::new(path.as_ref()).exists()
}

/// 获取文件大小（字节）
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::get_file_size;
/// use std::fs;
/// 
/// // 创建临时文件用于测试
/// fs::write("temp_test_file.txt", "hello").unwrap(); // 5 bytes
/// 
/// let size = get_file_size("temp_test_file.txt").unwrap();
/// assert_eq!(size, 5);
/// 
/// // 清理
/// std::fs::remove_file("temp_test_file.txt").unwrap();
/// ```
pub fn get_file_size<P: AsRef<Path>>(path: P) -> Result<u64, std::io::Error> {
    let metadata = fs::metadata(path)?;
    Ok(metadata.len())
}

/// 读取文件内容
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::read_file;
/// use std::fs;
/// 
/// // 创建临时文件用于测试
/// fs::write("temp_test_file.txt", "hello world").unwrap();
/// 
/// let content = read_file("temp_test_file.txt").unwrap();
/// assert_eq!(content, "hello world");
/// 
/// // 清理
/// std::fs::remove_file("temp_test_file.txt").unwrap();
/// ```
pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String, std::io::Error> {
    fs::read_to_string(path)
}

/// 写入文件内容
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::write_file;
/// 
/// write_file("temp_test_file.txt", "hello world").unwrap();
/// 
/// let content = std::fs::read_to_string("temp_test_file.txt").unwrap();
/// assert_eq!(content, "hello world");
/// 
/// // 清理
/// std::fs::remove_file("temp_test_file.txt").unwrap();
/// ```
pub fn write_file<P: AsRef<Path>>(path: P, content: &str) -> Result<(), std::io::Error> {
    fs::write(path, content)
}

/// 获取系统信息
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::system_utils::get_system_info;
/// 
/// let info = get_system_info();
/// assert!(!info.os_type.is_empty());
/// ```
pub fn get_system_info() -> SystemInfo {
    SystemInfo {
        os_type: env::consts::OS.to_string(),
        arch: env::consts::ARCH.to_string(),
        current_dir: current_dir().unwrap_or_else(|_| "Unknown".to_string()),
    }
}

/// 系统信息结构体
#[derive(Debug, Clone)]
pub struct SystemInfo {
    /// 操作系统类型
    pub os_type: String,
    /// 架构
    pub arch: String,
    /// 当前目录
    pub current_dir: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_get_env_var() {
        // 测试获取存在的环境变量
        let path = get_env_var("PATH");
        assert!(path.is_some());
        
        // 测试获取不存在的环境变量
        let nonexistent = get_env_var("NONEXISTENT_VAR_12345");
        assert!(nonexistent.is_none());
    }

    #[test]
    fn test_set_env_var() {
        set_env_var("TEST_VAR_RUST_UTILS", "test_value");
        assert_eq!(env::var("TEST_VAR_RUST_UTILS").unwrap(), "test_value");
    }

    #[test]
    fn test_current_dir() {
        let dir = current_dir();
        assert!(dir.is_ok());
    }

    #[test]
    fn test_file_operations() {
        let test_file = "test_file_sys_utils.txt";
        let test_content = "Hello, system utils!";
        
        // 测试文件写入
        assert!(write_file(test_file, test_content).is_ok());
        
        // 测试文件存在性检查
        assert_eq!(file_exists(test_file), true);
        
        // 测试文件大小获取
        let size = get_file_size(test_file).unwrap();
        assert_eq!(size, test_content.len() as u64);
        
        // 测试文件读取
        let content = read_file(test_file).unwrap();
        assert_eq!(content, test_content);
        
        // 清理测试文件
        fs::remove_file(test_file).unwrap();
        assert_eq!(file_exists(test_file), false);
    }

    #[test]
    fn test_get_system_info() {
        let info = get_system_info();
        assert!(!info.os_type.is_empty());
        assert!(!info.arch.is_empty());
        assert!(!info.current_dir.is_empty());
    }
}